package jy.game.components.save;


import java.util.ArrayList;
import java.util.List;

import jy.game.components.item.JyItem;
import jy.game.components.item.JyItemInstance;
import jy.game.components.role.JyRole;
import jy.game.components.wuxue.JyTrigger;
import jy.game.components.wuxue.box.JyInternalSkillInstance;
import jy.game.components.wuxue.box.JySkillInstance;
import jy.game.components.wuxue.box.JySpecialSkillInstance;
import jy.game.components.wuxue.pojo.JyInternalSkill;
import jy.game.components.wuxue.pojo.JySkill;
import jy.game.components.wuxue.pojo.JySpecialSkill;
import jy.game.tools.CommonSettings;
import jy.game.tools.JyResourceManager;
import wx.xml.simpleframework.xml.Attribute;
import wx.xml.simpleframework.xml.Element;

// [XmlType]
public class JyGameSaveRole {

    @Attribute public String                key;
    @Attribute public String                animation;
    @Attribute public String                name;
    @Attribute public String                head;
    @Attribute public int                   maxhp;
    @Attribute public int                   maxmp;
    @Attribute public int                   wuxing;
    @Attribute public int                   shenfa;
    @Attribute public int                   bili;
    @Attribute public int                   gengu;
    @Attribute public int                   fuyuan;
    @Attribute public int                   dingli;
    @Attribute public int                   quanzhang;
    @Attribute public int                   jianfa;
    @Attribute public int                   daofa;
    @Attribute public int                   qimen;
    @Attribute public String                currentSkillName;
    @Attribute public int                   exp;
    @Attribute public int                   female;
    @Attribute public int                   leftpoint;
    @Attribute public String                grow_template;
    @Attribute public int                   level;
    @Attribute public String                talent;
    @Element(name = "sk")
    public            JyGameSaveRoleSkill[] skills;
    @Element(name = "i")
    public            JyGameSaveRoleSkill[] internalSkills;
    @Element(name = "sp")
    public            JyGameSaveRoleSkill[] specialSkills;
    @Element(name = "e")
    public            JyGameSaveItem[]      equippments;
    @Attribute public int                   jiushen_count;

    public static JyGameSaveRole[] Create(List<JyRole> roles) {
        JyGameSaveRole[] array = new JyGameSaveRole[roles.size()];
        for (int i = 0; i < roles.size(); i++) {
            array[i] = JyGameSaveRole.Create(roles.get(i));
        }
        return array;
    }

    public static JyGameSaveRole Create(JyRole r) {
        JyGameSaveRole gameSaveRole = new JyGameSaveRole();
        gameSaveRole.key = r.key;
        gameSaveRole.animation = r.animation;
        gameSaveRole.name = r.name;
        gameSaveRole.head = r.head;
        gameSaveRole.maxhp = r.maxhp;
        gameSaveRole.maxmp = r.maxmp;
        gameSaveRole.wuxing = r.wuxing;
        gameSaveRole.shenfa = r.shenfa;
        gameSaveRole.bili = r.bili;
        gameSaveRole.gengu = r.gengu;
        gameSaveRole.fuyuan = r.fuyuan;
        gameSaveRole.dingli = r.dingli;
        gameSaveRole.quanzhang = r.quanzhang;
        gameSaveRole.jianfa = r.jianfa;
        gameSaveRole.daofa = r.daofa;
        gameSaveRole.qimen = r.qimen;
        gameSaveRole.currentSkillName = r.currentSkillName;
        gameSaveRole.exp = r.exp;
        gameSaveRole.female = r.femaleValue;
        gameSaveRole.leftpoint = r.leftpoint;
        gameSaveRole.grow_template = r.growTemplateValue;
        gameSaveRole.level = r.level();
        gameSaveRole.talent = r.talentValue;//TalentValue();
        if (r.skills.size() > 0) {
            gameSaveRole.skills = new JyGameSaveRoleSkill[r.skills.size()];
            for (int i = 0; i < r.skills.size(); i++) {
                gameSaveRole.skills[i] = JyGameSaveRoleSkill.Create(r.skills.get(i));
            }
        }
        if (r.internalSkills.size() > 0) {
            gameSaveRole.internalSkills = new JyGameSaveRoleSkill[r.internalSkills.size()];
            for (int j = 0; j < r.internalSkills.size(); j++) {
                gameSaveRole.internalSkills[j] = JyGameSaveRoleSkill.Create(r.internalSkills.get(j));
            }
        }
        if (r.specialSkills.size() > 0) {
            gameSaveRole.specialSkills = new JyGameSaveRoleSkill[r.specialSkills.size()];
            for (int k = 0; k < r.specialSkills.size(); k++) {
                gameSaveRole.specialSkills[k] = JyGameSaveRoleSkill.Create(r.specialSkills.get(k));
            }
        }
        if (r.equipment.size() > 0) {
            gameSaveRole.equippments = new JyGameSaveItem[r.equipment.size()];
            for (int l = 0; l < r.equipment.size(); l++) {
                JyItemInstance itemInstance = r.equipment.get(l);
                JyGameSaveItem gameSaveItem = new JyGameSaveItem(
                    itemInstance.name,
                    itemInstance.additionTriggers.toArray(new JyTrigger[0]),
                    1
                );
                gameSaveRole.equippments[l] = gameSaveItem;
            }
        }
        if (CommonSettings.MOD_KEY >= 0) {
            gameSaveRole.jiushen_count = r.jiushen_count;
        }
        int num = CommonSettings.MAX_ATTRIBUTE;
        if (gameSaveRole.wuxing > num) {
            gameSaveRole.wuxing = num;
        }
        if (gameSaveRole.shenfa > num) {
            gameSaveRole.shenfa = num;
        }
        if (gameSaveRole.bili > num) {
            gameSaveRole.bili = num;
        }
        if (gameSaveRole.gengu > num) {
            gameSaveRole.gengu = num;
        }
        if (gameSaveRole.fuyuan > num) {
            gameSaveRole.fuyuan = num;
        }
        if (gameSaveRole.dingli > num) {
            gameSaveRole.dingli = num;
        }
        if (gameSaveRole.quanzhang > num) {
            gameSaveRole.quanzhang = num;
        }
        if (gameSaveRole.jianfa > num) {
            gameSaveRole.jianfa = num;
        }
        if (gameSaveRole.daofa > num) {
            gameSaveRole.daofa = num;
        }
        if (gameSaveRole.qimen > num) {
            gameSaveRole.qimen = num;
        }
        return gameSaveRole;
    }

//    public JyRole GenerateRole() {
//        JyRole role = new JyRole();
//        role.key = this.key;
//        role.animation = this.animation;
//        role.name = this.name;
//        role.head = this.head;
//        role.maxhp = this.maxhp;
//        role.hp = this.maxhp;
//        role.maxmp = this.maxmp;
//        role.mp = this.maxmp;
//        role.wuxing = this.wuxing;
//        role.shenfa = this.shenfa;
//        role.bili = this.bili;
//        role.gengu = this.gengu;
//        role.fuyuan = this.fuyuan;
//        role.dingli = this.dingli;
//        role.quanzhang = this.quanzhang;
//        role.jianfa = this.jianfa;
//        role.daofa = this.daofa;
//        role.qimen = this.qimen;
//        role.currentSkillName = this.currentSkillName;
//        role.femaleValue = this.female;
//        role.leftpoint = this.leftpoint;
//        role.growTemplateValue = this.grow_template;
//        role.setLevel(this.level);
//        role.exp = this.exp;
//        role.setTalentValue(this.talent);
//        role.skills = this.GetSkills();
//        role.internalSkills = this.GetInternalSkills();
//        role.specialSkills = this.GetSpecialSkills();
//        if (this.equippments != null) {
//            for (JyGameSaveItem gameSaveItem : this.equippments) {
//                if (JyResourceManager.get(JyItem.class, gameSaveItem.name) != null) {
//                    JyItemInstance item = gameSaveItem.Generate();
//                    role.equipment.add(item);
//                }
//            }
//        }
//        if (CommonSettings.MOD_KEY >= 0) {
//            role.jiushen_count = this.jiushen_count;
//        }
//        int num = CommonSettings.MAX_ATTRIBUTE;
//        if (role.wuxing > num) {
//            role.wuxing = num;
//        }
//        if (role.shenfa > num) {
//            role.shenfa = num;
//        }
//        if (role.bili > num) {
//            role.bili = num;
//        }
//        if (role.gengu > num) {
//            role.gengu = num;
//        }
//        if (role.fuyuan > num) {
//            role.fuyuan = num;
//        }
//        if (role.dingli > num) {
//            role.dingli = num;
//        }
//        if (role.quanzhang > num) {
//            role.quanzhang = num;
//        }
//        if (role.jianfa > num) {
//            role.jianfa = num;
//        }
//        if (role.daofa > num) {
//            role.daofa = num;
//        }
//        if (role.qimen > num) {
//            role.qimen = num;
//        }
//        role.initBind();
//        return role;
//    }

//    public List<JySkillInstance> GetSkills() {
//        List<JySkillInstance> list = new ArrayList<JySkillInstance>();
//        if (this.skills == null) {
//            return list;
//        }
//        for (JyGameSaveRoleSkill gameSaveRoleSkill : this.skills) {
//            if (JyResourceManager.get(JySkill.class, gameSaveRoleSkill.name) != null) {
//                list.add(gameSaveRoleSkill.GenerateSkill());
//            }
//        }
//        return list;
//    }

//    public List<JyInternalSkillInstance> GetInternalSkills() {
//        List<JyInternalSkillInstance> list = new ArrayList<>();
//        if (this.internalSkills == null) {
//            return list;
//        }
//        for (JyGameSaveRoleSkill gameSaveRoleSkill : this.internalSkills) {
//            if (JyResourceManager.get(JyInternalSkill.class, gameSaveRoleSkill.name) != null) {
//                list.add(gameSaveRoleSkill.GenerateInternalSkill());
//            }
//        }
//        return list;
//    }
//
//    public List<JySpecialSkillInstance> GetSpecialSkills() {
//        List<JySpecialSkillInstance> list = new ArrayList<>();
//        if (this.specialSkills == null) {
//            return list;
//        }
//        for (JyGameSaveRoleSkill gameSaveRoleSkill : this.specialSkills) {
//            if (JyResourceManager.get(JySpecialSkill.class, gameSaveRoleSkill.name) != null) {
//                list.add(gameSaveRoleSkill.GenerateSpecialSkill());
//            }
//        }
//        return list;
//    }
}
